package com.itboye.eweilisten.me.timu;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.timu.adapter.WanXingAdapter;
import com.itboye.eweilisten.model.DanXuanBean;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.model.TiXingBean;
import com.itboye.eweilisten.presenter.TiMuPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.ZuoTiPopupWindow;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 完形填空
 */
public class WanXingActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    TextView zuoti_title, zuoti_title01;
    TextView zuoti_zidong;
    TextView zuoti_shoudong;

    RecyclerView recyclerView;

    TiMuPresenter tiMuPresenter;

    ArrayList<String> questions = new ArrayList<String>();//内容
    ArrayList<TIbean.Options> answers = new ArrayList<TIbean.Options>();//答案前缀
    ArrayList<String> answerss = new ArrayList<String>();//答案内容
    ArrayList<DanXuanBean> beans = new ArrayList<DanXuanBean>();
    List<TIbean> beanTi = new ArrayList<>();

    WanXingAdapter adapter;
    TiXingBean bean;
    ZuoTiPopupWindow zuoTiPopupWindow;//弹出框


    ArrayList<String> answers_nei;//子类答案内容
    ArrayList<TIbean.Options> answers_qian;//子类答案前缀
    List<TIbean.Clild> cc = new ArrayList<TIbean.Clild>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wanxing);

        tvTitle.setText("单项选择");
        bean = new TiXingBean();
        recyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        tiMuPresenter = new TiMuPresenter(this);
        tiMuPresenter.onXiangQing("171", "14", "itboye");
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                zuoTiPopupWindow = new ZuoTiPopupWindow(WanXingActivity.this);
                break;
            case R.id.zuoti_shoudong:
                StringBuffer sb = new StringBuffer();

                for (int i = 0; i < beanTi.size(); i++) {
                    for (int i1 = 0; i1 < cc.size(); i1++) {
                        sb.append(cc.get(i1).getId() + ":" + cc.get(i1).getXuan() + ",");
                    }
                }
                System.out.println(sb.toString() + "提交答案");
                MAlert.alert(sb.substring(0, sb.length() - 1).toString());

//                String id = (String) SPUtils.get(WanXingActivity.this, null, Const.ID, "");
//                showProgressDialog("正在提交,请稍后...",true);
//                tiMuPresenter.onTiJiao(id,"14","55",sb.substring(0, sb.length() - 1).toString());

                break;
        }
    }

    @Override
    public void update(Observable observable, Object o) {
        ResultEntity entity = handlerError(o);
        if (entity != null) {
            if (entity.getEventType() == tiMuPresenter.zuoti_xq_success) {
                bean = (TiXingBean) entity.getData();
                zuoti_title.setText(bean.getName());
                zuoti_title01.setText(bean.getParent_name());

                List<TiXingBean.ListInfo> list = new ArrayList<TiXingBean.ListInfo>();
                list.addAll(bean.getList());

                Log.d("tixingcuowu", "" + list);
                for (int i = 0; i < list.size(); i++) {
                    questions = new ArrayList<>();
                    answers = new ArrayList<>();
                    answerss = new ArrayList<>();
                    TIbean tib = new TIbean();
                    int index=0;
                    //内容
                    for (int j = 0; j < list.get(i).getContent().get(0).size(); j++) {
                        if ("con".equals(list.get(i).getContent().get(0).get(j)[0] + "")) {
                            //___
                            index++;
                            questions.add(index+"_");
                        } else if ("str".equals(list.get(i).getContent().get(0).get(j)[0] + "")) {
                            //有文字
                            questions.add(list.get(i).getContent().get(0).get(j)[1] + "");
                        }
                    }
                    tib.setContent(questions);
                    List<TiXingBean.ListInfo.Child> child = list.get(i).getChild();

                    for (int i1 = 0; i1 < child.size(); i1++) {
                        answers_nei = new ArrayList<String>();//答案内容
                        answers_qian = new ArrayList<TIbean.Options>();//答案前缀
                        TIbean.Clild cl = new TIbean.Clild();
                        //答案内容
                        for (int j = 0; j < child.get(i1).getAnswer().size(); j++) {
                            if ("str".equals(child.get(i1).getAnswer().get(j)[1] + "")) {
                                answers_nei.add(child.get(i1).getAnswer().get(j)[2] + "");
                            }
                        }
                        cl.setAnswers(answers_nei);
                        //答案前缀
                        for (int j = 0; j < child.get(i1).getAnswer().size(); j++) {
                            TIbean.Options op = new TIbean.Options();
                            if ("str".equals(child.get(i1).getAnswer().get(j)[1] + "")) {
                                op.setOptions(child.get(i1).getAnswer().get(j)[0] + "");
                                op.setDa(false);
                                answers_qian.add(op);
                            }
                        }
                        cl.setAnwser_true(child.get(i1).getAnswer_true());
                        cl.setId(child.get(i1).getId());
                        cl.setOption(answers_qian);
                        cc.add(cl);
                    }
                    tib.setClild(cc);
                    beanTi.add(tib);
                }
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                if (adapter == null) {
                    adapter = new WanXingAdapter(WanXingActivity.this, beanTi);
                    recyclerView.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }
                CountDownTimer cdt = new CountDownTimer(Integer.parseInt(bean.getTime_limit() + "") * 60 * 1000, 1000) {
                    //                    CountDownTimer cdt = new CountDownTimer(5000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        zuoti_zidong.setText("自动提交：" + millisUntilFinished / 1000 + "″");
                    }

                    @Override
                    public void onFinish() {

                    }
                };
                cdt.start();
            } else if (entity.getEventType() == tiMuPresenter.zuoti_xq_fail) {
                MAlert.alert(entity.getData(), Gravity.TOP);
            }
            if (entity.getEventType() == tiMuPresenter.tijiao_success) {
                MAlert.alert(entity.getData() + "", Gravity.TOP);
            }
            if (entity.getEventType() == tiMuPresenter.tijiao_fail) {
                MAlert.alert(entity.getData() + "", Gravity.TOP);
            }
            try {
                closeProgressDialog();
            } catch (Exception e) {
            }
        }

    }
}
